package ddemo.viewAndcontroller;

import ddemo.pojo.Pbooking;
import ddemo.pojo.User;
import ddemo.pojo.Cbooking;
import ddemo.service.CbookService;
import ddemo.service.PbookingService;

import java.util.*;

public class bookingView {
    public static void choose(User user){
        System.out.println("------------------------------------------------");
        System.out.println("1.充电预约");
        System.out.println("2.停车预约");
        System.out.println("3.返回上一级");
        System.out.println("------------------------------------------------");
        String mnuenum=new Scanner(System.in).next();
        if("1".equals(mnuenum)){
            Cbooking(user);

        }
        else if("2".equals(mnuenum)){
            Pbooking(user);
        }
        else if("3".equals(mnuenum)){
            return;
        }
        else{
            System.out.println("输入错误，请输入正确的数字");
            System.out.println("--------------------");
        }
    }

    private static void Pbooking(User user) {
        System.out.println("选择你要预约的时间(只可预约次日)");
        List<Pbooking> Pbookings = PbookingService.getAll();
        TreeSet<String> times=new TreeSet<String>();
        for(Pbooking pbooking : Pbookings){
            times.add(pbooking.getTime());
        }
        System.out.println("可预约的时间段");
        for(String time : times){
            System.out.println(time);
        }
        String chooseTime=new Scanner(System.in).next();
        if(!times.contains(chooseTime)){
            System.out.println("输入的时间段错误,请输入正确的数据");
            Cbooking(user);
            return;
        }
        Set<String> ids=new HashSet<String>();
        System.out.println("这个时间段可预约的停车位");
        for(Pbooking pbooking : Pbookings){
            if(chooseTime.equals(pbooking.getTime()) && "可用".equals(pbooking.getUsestatus())){
                System.out.println("停车位id:"+pbooking.getId()+"车位所在充电站:"+pbooking.getLotid());
                ids.add(pbooking.getId().toString());
            }
        }
        System.out.println("选择您要选择的车位id");
        String id=new Scanner(System.in).next();
        if(!ids.contains(id)){
            System.out.println("未找到该车位或输入错误,请重试");
            return;
        }
        System.out.println(        PbookingService.book(Integer.parseInt(id),user,chooseTime));
        System.out.println("完成预约");
        System.out.println("3S后跳转");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void Cbooking(User user) {
        System.out.println("选择你要预约的时间(只可预约次日)");
        List<Cbooking> Cbookings = CbookService.getAll();
        TreeSet<String> times=new TreeSet<String>();
        for(Cbooking Cbooking : Cbookings){
            times.add(Cbooking.getTime());
        }
        System.out.println("可预约的时间段");
        for(String time : times){
            System.out.println(time);
        }
        String chooseTime=new Scanner(System.in).next();
        if(!times.contains(chooseTime)){
            System.out.println("输入的时间段错误,请输入正确的数据");
            Cbooking(user);
            return;
        }
        Set<String> ids=new HashSet<String>();
        System.out.println("这个时间段可预约的充电口");
        for(Cbooking Cbooking : Cbookings){
            if(Cbooking.getTime()==chooseTime&&"可用".equals(Cbooking.getUsestatus())){
                System.out.println("充电口id:"+Cbooking.getId()+"充电口所在充电桩:"+Cbooking.getChargerid());
                ids.add(Cbooking.getId().toString());
            }
        }
        System.out.println("选择您要选择的充电口id");
        String id=new Scanner(System.in).next();
        if(!ids.contains(id)){
            System.out.println("未找到该充电口");
        }
        CbookService.book(Integer.parseInt(id),user,chooseTime);
        System.out.println("完成预约");
        System.out.println("3S后跳转");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

}
